﻿@using  Layui.Framework.Datatable
@using Layui.Entities
@model DataSourceResult<SysUser, SysUserSearchArg>
@{
    ViewData["Title"] = "系统用户列表";
    bool canAddOrUpdate = WorkContext.OwnPermission("editUser");
    bool canResetPwd = WorkContext.OwnPermission("resetPassword");
    bool canDelete = WorkContext.OwnPermission("deleteUser");
    bool canSetUserRole = WorkContext.OwnPermission("setUserRole");
}

<div class="breadcrumbs" id="breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="ace-icon fa fa-home home-icon"></i>
            <a href="@Url.RouteUrl("mainIndex")">首页</a>
        </li>
        <li class="active">系统用户</li>
    </ul>
</div>
<div class="page-content">
    <div class="page-header">
        <h1>
            @ViewData["Title"]
        </h1>
    </div>
    <div class="row">
        <div class="col-sm-12">
            @if (canAddOrUpdate)
            {
                <a asp-route="editUser" class="btn btn-sm btn-pink btnEditUser"><i class="glyphicon glyphicon-plus"></i>添加系统用户</a>
            }
        </div>
    </div>
    <div class="space-2"></div>
    <div class="row">
        <div class="col-sm-12">
            <div class="panel panel-default">
                <div class="panel-body">
                    <form asp-route="userIndex" method="get" class="form-inline">
                        <label>关键字</label>
                        <input type="text" class="form-control" name="q" style="width:220px;" value="@Model.Paging.RouteArg.q" placeholder="账号、姓名、邮箱、手机号" />
                        <button type="submit" class="btn btn-purple btn-sm">
                            查询
                            <i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <table id="user_datatable" class="table table-striped table-bordered table-hover dataTable no-footer">
                <thead>
                    <tr>
                        <th width="150px">账号</th>
                        <th width="100px">姓名</th>
                        <th width="200px">所属角色</th>
                        <th width="60px">性别</th>
                        <th width="150px">活动时间</th>
                        <th width="100px">启用</th>
                        <th width="120px">登录锁</th>
                        <th width="50px;">密码</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model.Data)
                    {
                        <tr>
                            <td>@item.Account</td>
                            <td>@item.Name</td>
                            <td>
                                @if (item.SysRoles != null)
                                {
                                    @(String.Join('，', item.SysRoles.Select(x => x.Name)))
                                }
                            </td>
                            <td>@item.Sex</td>
                            <td>@(item.LastActivityTime?.ToString("yyyy-MM-dd HH;mm"))</td>
                            <td>
                                @if (!item.IsAdmin)
                                {
                                    if (item.Enabled)
                                    {
                                        <span>@(item.Enabled ? "是" : "禁用")</span>
                                    }
                                }
                            </td>
                            <td>
                                @if (item.LoginLock)
                                {
                                    <label>
                                        <input name="lock" value="@item.Id" class="ace ace-switch ace-switch-5 user_lock" type="checkbox" checked="checked" />
                                        <span class="lbl"></span>
                                    </label>
                                    if (item.AllowLoginTime.HasValue && item.AllowLoginTime > DateTime.Now)
                                    {
                                        <span class="badge">@((int)(item.AllowLoginTime.Value - DateTime.Now).TotalMinutes + 1)分</span>
                                    }
                                }
                                else
                                {
                                    <label data-toggle="tooltip" data-placement="left" title="Tooltip on left">
                                        <input name="lock" value="@item.Id" class="ace ace-switch ace-switch-5 user_lock" type="checkbox" />
                                        <span class="lbl"></span>
                                    </label>
                                }
                            </td>
                            <td>
                                @if (!item.IsAdmin)
                                {
                                    if (canResetPwd)
                                    {
                                        <a href="@Url.RouteUrl("resetPassword", new { id = item.Id })" class="btn btn-xs btn-warning btnResetPwd" title="重置密码"><i class="ace-icon fa fa-undo"></i></a>
                                    }
                                }
                            </td>
                            <td>
                                @if (canAddOrUpdate)
                                {
                                    <a href="@Url.RouteUrl("editUser", new { id = item.Id })" class="btn btn-xs btn-primary btnEditUser"><i class="fa fa-pencil-square-o"></i></a>
                                }
                                @if (canDelete)
                                {
                                    if (!item.IsAdmin)
                                    {
                                        <a href="@Url.RouteUrl("deleteUser", new { id = item.Id })" class="btn btn-xs btn-danger btnDelete"><i class="glyphicon glyphicon-trash"></i></a>
                                    }
                                }
                                @if (canSetUserRole)
                                {
                                    if (!item.IsAdmin)
                                    {
                                        <a href="@Url.RouteUrl("setUserRole", new { id = item.Id })" class="btn btn-xs btn-purple btnSetRole">设置角色</a>
                                    }
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            <pager page-value="Model.Paging"></pager>

        </div>
    </div>
</div>
<script>
    $(function () {

        $('.btnEditUser').click(function (e) {
            e.preventDefault();
            var _url = this.href;
            layer.load(2);
            $.get(_url, function (html) {
                layer.closeAll();
                layer.open({
                    type: 1,
                    title: '编辑用户',
                    area: ['600px', '400px'],
                    content: html
                });
            });

        });

        $('.btnSetRole').click(function (e) {
            e.preventDefault();
            var _url = this.href;
            layer.load(2);
            $.get(_url, function (html) {
                layer.closeAll();
                layer.open({
                    type: 1,
                    title: '设置角色',
                    shadeClose: false,
                    area: ['600px', '400px'],
                    content: html
                });
            });

        });


        $('.btnDelete').click(function (e) {
            e.preventDefault();
            var url = this.href;
            layer.confirm('您确定要删除此用户？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                layer.load(2);
                $.get(url, function (data) {
                    if (data.Status) {
                        location.reload();
                    } else {
                        layer.msg(data.Message, { icon: 2 });
                    }
                });
            });
        });

        $('.btnResetPwd').click(function (e) {
            e.preventDefault();
            var url = this.href;
            layer.confirm('您确定要重置用户密码？', {
                btn: ['确定', '取消']
            }, function () {
                layer.load(2);
                $.get(url, function (data) {
                    layer.closeAll();
                    if (data.Status) {
                       layer.msg(data.Message, { icon: 1 });
                    } else {
                        layer.msg(data.Message, { icon: 2 });
                    }
                });
            });
        });

         $('.user_lock').on('click', function () {
             //锁定与解锁
             $.get('@Url.RouteUrl("loginLock")', { id: $(this).val(), loginLock: this.checked }, function (data) {
                 layer.msg(data.Message, { icon: 1 });
                });
            });
    });
</script>


